<template>
	<view class="peo_box">
		<navBar title="选择管理人员"></navBar>
		<view style="padding: 20rpx 40rpx;">
			<searchCom class="search-input" v-model="searchText" @enter="queryBtn" @change="queryBtn" @clear="blurBtn"></searchCom>
		</view>
		<scroll-view scroll-y="true" style="height:calc(100vh - 490rpx);">
			<view class="scroll-item">
				<view class="list-box">
					<u-checkbox-group placement="column" @change="checkboxChange">
						<view class="user-box" v-for="(item,index) in userList" :key="item.id">
							<view>
								<view class="name">{{ item.name2 }}</view>
							</view>
							<view class="right-box">
								<u-checkbox :name="item.id" :checked="item.isCheck" shape="circle"></u-checkbox>
							</view>
						</view>
					</u-checkbox-group>
				</view>
			</view>
		</scroll-view>
		<view class="footer-button">
			<view class="button-placeholder"></view>
			<view class="button-group">
				<button @click="saveUser"  class="button-primary">保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		standardDataList,
		personGetListByOrgId
	} from '@/api/comprehensive/comprehensive.js'


	import mixins from "@/utils/mixins.js"
	import searchCom from "@/components/searchCom.vue"
	export default {
		name: "dispatchPerson",
		mixins: [mixins],
		components: {
			searchCom
		},
		data() {
			return {
				searchText: '',
				userList: [] //所有用户列表
			}
		},
		watch: {
			searchText(newValue, oldValue) {
				// if(newValue) {

				// this.userList = this.userList.filter(item => item.name.indexOf(newValue) !== -1)
				// }
				// if (newValue !== oldValue) {
				// 	this.getUserList()
				// }
			}
		},
		onLoad(option) {
			const eventChannel = this.getOpenerEventChannel();
			// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
			eventChannel.on('getList', (data) => {
				console.log("传过来的数组", data)
				this.checkUserList = data.data || []
				this.getUserList()
			})
			this.eventChannel = eventChannel

		},
		methods: {
			queryBtn(val) {
				console.log(val, '事件')
				this.searchText = val
				this.getUserList()
				// this.userList = this.userList.filter(item => item.name2.indexOf(val) !== -1)
			},
			blurBtn(val) {
				this.searchText = ''
				this.getUserList()
			},
			// 获取人员数据(项目经理)
			async getUserList() {
				// console.log(this.$store.getters, 'vuex70')
				let list1 = this.$store.getters.dictMap['standard_data_type']
				console.log(list1, '9999')
				let type = list1.find(item => item.code === "engineering_structure").code || "engineering_structure"
				let _query2 = {
					currPage: 1,
					pageSize: 10000,
					params: {
						type: type
					}
				}
				//  获取标准数据
				let list = await standardDataList(_query2)
				let businessTypeList = []
				if (list.success && list.status == 1) {
					businessTypeList = list.result.list.filter(item => item.treeLevel === 1)
				}
				let t = businessTypeList.find(item => item.code === 'zhzzdx').id
				//  过滤人员类型
				let userType = this.$store.getters.dictMap['person_type']
				let typeIds;
				if(userType) {
					typeIds = userType.filter(it => it.code === 'zc' || it.code === 'lwpq' || it.code === 'rsdl').map(ma => ma.id).join(',')
					console.log(typeIds,'105行')
				}
				// zhzzdx
				let _query = {
					currPage: 1,
					pageSize: 10000,
					params: {
						standardDataIds: [t],
						name: this.searchText,
						typeIds,
						projectId:uni.getStorageSync('isOwner')
					}
				}

				let result = await personGetListByOrgId(_query)
				// console.log(JSON.stringify(result), 'result')
				this.userList = result.result.list.map(item => ({
					...item,
					name2: item.name + ' (' + item.phone + ")"
				})) || []

				this.filterUserList()
				console.log(this.checkUserList, '---yonghu1');
			},
			// 过滤选中的用户
			filterUserList() {
				this.userList.forEach(item => {
					if (this.checkUserList.includes(item.id)) {
						item.isCheck = true
					}
				})
			},

			checkboxChange(array) {
				this.checkUserList = array || []
			},
			saveUser() {
				const arr = this.checkUserList || [];
				console.log(arr.length, 'arr')
				if (arr.length === 0) {
					return uni.showToast({
						title: '至少选择一个用户',
						icon: 'none',
						duration: 2000
					})
				}
				let userList = this.userList.filter(item => arr.includes(item.id))
				uni.navigateBack({
					success: () => {
						this.eventChannel.emit("setData", {
							data: userList
						})
					}
				});

			}

		}
	}
</script>

<style lang="scss" scoped>
	.peo_box {
		/* #ifdef MP-WEIXIN */
			padding-top: 180rpx;
		/* #endif */
	}
	.gb-scroll-box {
		.scroll-item {
			background-color: #fff;
			border-top: 40rpx solid #F5F7FAFF;
			padding: 0 40rpx 0;

		}

		.list-box {
			.right-box {
				padding: 12rpx;
				border-radius: 14rpx;
			}

			.list-box-item {
				align-items: end;
				padding: 40rpx 0 30rpx;
				border-bottom: 1px solid #F5F7FAFF;
			}

			.list-box-item:last-child {
				border-bottom-width: 0rpx;
			}

			.name {
				font-size: 30rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #22204E;
				line-height: 42rpx;
			}

			.time {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #72708D;
				margin-top: 20rpx;
			}
		}
	}
	.user-box {
		    display: flex;
		    align-items: center;
		    justify-content: space-between;
		    padding: 30rpx 40rpx;
		    border-bottom: 1px solid #e7e6ee;
			font-size: 30rpx;
	}
</style>
